'''
    filter.py

        The filter.py module contains just one class called 'filter'
        which is used to tie together the backend datbase manager with
        the front end database search/filter tool
    
        filter

            The 'filter' class uses the db_interfacer class to allow
            the student to search for courses by filtering the course
            database using a course major, a keyword, or by classes
            the student can take or has not taken yet
'''

import db_interfacer
import course

'''
    filter

    The 'filter' class uses the db_interfacer class to allow
    the student to search for courses by filtering the course
    database using a course major, a keyword, or by classes
    the student can take or has not taken yet
'''
class filter():

    '''
        __init__()

        Initializes a filter class with the student class
        and the starting year
    '''
    def __init__(self, student):
        self.student = student
        self.year = 0

    '''
        get_courses()

        Returns the courses that were filtered
    '''
    def get_courses(self, booleans, year):
        gep, major, search, untaken, can_take = booleans
        #courses = self.db.get_courses(gep, major)
        courses = db_interfacer.filter(major, gep)
        #print major, gep
        eligible_courses = []
        for returned_course in courses:
            eligible_courses.append(course.course(returned_course))
        if untaken:
            eligible_courses = self.filter_by_has_taken(eligible_courses)
        if can_take:
            eligible_courses = self.filter_by_can_take(eligible_courses, year)
        eligible_courses = self.filter_by_keyword(search, eligible_courses)
        return eligible_courses
    
    '''
        filter_by_has_taken()

        Filters the course database by the courses 
        the student has not taken yet
    '''
    def filter_by_has_taken(self, classes):
        untaken_courses = []
        for course in classes:
            if not self.student.has_taken(course):
                untaken_courses.append(course)
        return untaken_courses
    
    '''
        filter_by_can_take()

        Filters the course database by the courses the student
        can take
    '''
    def filter_by_can_take(self,classes, year):
        valid_courses = []
        for course in classes:
           if len(self.student.get_valid_semesters(course, year)) > 0:
               valid_courses.append(course)
        return valid_courses
    
    '''
        filter_by_keyword()  

        Filters the course database by keyword
    '''
    def filter_by_keyword(self, searchterm, classes):
        valid_courses = []
        for course in classes:
           if course.contains_searchterm(searchterm):
              valid_courses.append(course)
        return valid_courses
             
